package com.tenebrous.jestic.service.framwork.system;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tenebrous.jestic.entity.framwork.meber.organ.OrgOrganize;
import com.tenebrous.jestic.entity.framwork.system.SysLogLogin;
import com.tenebrous.jestic.entity.framwork.system.SysLogModify;
import com.tenebrous.jestic.entity.framwork.system.SysLogOper;
import com.tenebrous.jestic.mapper.idao.framwork.meber.organ.OrgOrganizeMapper;
import com.tenebrous.jestic.mapper.idao.framwork.system.SysLogLoginMapper;
import com.tenebrous.jestic.mapper.idao.framwork.system.SysLogModifyMapper;
import com.tenebrous.jestic.mapper.idao.framwork.system.SysLogOperMapper;
import com.tenebrous.jestic.service.SuperService;
import com.rain.ops.sdk.dto.ApiResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SysLogService extends SuperService<SysLogLogin> {

    @Autowired
    private SysLogLoginMapper mapper;

    @Autowired
    private SysLogOperMapper operlogMapper;

    @Autowired
    private SysLogModifyMapper modifyMapper;

    @Autowired
    private OrgOrganizeMapper organizeMapper;

    @Override
    @Autowired
    protected void setSuperMapper() {
        super.setSuperMapper(this.mapper);
    }

    public SysLogOper selectOperByPrimaryKey(String id) {
        return this.operlogMapper.selectByPrimaryKey(id);
    }

    public PageInfo queryModifyLogPagingList(Integer pageIndex, Integer pageSize, String batch) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> this.modifyMapper.selectByCondition(batch));
    }

    public int saveOperLogSelective(SysLogOper item) {
        return this.operlogMapper.insertSelective(item);
    }

    /**
     * 回撤当前记录至变更前状态
     *
     * @param id
     * @return
     */
    public ApiResult revokeModifyLogItem(String id) throws ClassNotFoundException {
        SysLogModify mlog = this.modifyMapper.selectByPrimaryKey(id);
        if (mlog != null) {
            String cls = mlog.getSclass();
            String otype = mlog.getOtype();
            if (StringUtils.isNotBlank(cls) && StringUtils.isNotBlank(otype)) {
                String safter = mlog.getSafter();
                String sbefor = mlog.getSbefor();
                switch (otype) {
                    case "组织移动":
                    case "组织更名":
                    case "组织撤销":
                        Object target = JSON.parseObject(sbefor, Class.forName(cls));
                        this.organizeMapper.updateByPrimaryKeySelective((OrgOrganize) target);
                        break;
                    case "组织拆分": ;
                    case "组织合并": ;
                }
                mlog.setRevoked(1);
                this.modifyMapper.updateByPrimaryKeySelective(mlog);
                return new ApiResult(200, "撤回成功。");
            } else {
                return new ApiResult(999, "撤回失败，缺少属性值。");
            }
        } else {
            return new ApiResult(999, "撤回失败，找不到指定记录：" + id);
        }
    }

    /**
     * 查询登陆日志
     * @param loginName
     * @return
     */
    public PageInfo<SysLogLogin> queryLoginPagingList(Integer pageIndex, Integer pageSize, String loginName) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> this.mapper.selectByCondition(loginName));
    }

    /**
     * 清空登陆日志
     * @return
     */
    public ApiResult deleteAllLogin() {
        return new ApiResult(200, "操作成功。", this.operlogMapper.deleteAll());
    }

    /**
     * 分页查询操作日志
     * @param pageIndex
     * @param pageSize
     * @param username
     * @return
     */
    public PageInfo<SysLogOper> queryOperPagingList(Integer pageIndex, Integer pageSize, String username) {
        return this.queryOperPagingList(pageIndex, pageSize, username);
    }

    /**
     * 分页查询操作日志
     * @param pageIndex
     * @param pageSize
     * @param username
     * @param include 包含的账号
     * @param exclude 排查的账号
     * @return
     */
    public PageInfo<SysLogOper> queryOperPagingList(Integer pageIndex, Integer pageSize, String username, String[] include, String[] exclude) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> this.operlogMapper.selectByCondition(username, include, exclude));
    }

    /**
     * 清空操作日志
     * @return
     */
    public ApiResult deleteAllOper() {
        return new ApiResult(200, "操作成功。", this.operlogMapper.deleteAll());
    }
}
